<template>
  <div class="login" :class="isCofco?'cofco':'other'">
    <div style="display:flex;justify-content:space-between;align-items: center;">
      <div style="padding: 0 60px 0 0">
        <h3 style="color: #ffffff;font-size: 24px;padding: 0;margin-bottom: 10px" v-if="!isCofco">
          木瓜商贸管理平台
        </h3>
        <ul style="margin: 0;color: #ffffff;font-size: 15px;line-height: 25px;padding-left: 20px">
          <li>Springboot2</li>
          <li>MybatisPlus</li>
          <li>SpringSecurity</li>
          <li>Jwt</li>
          <li>Redis</li>
          <li>Vue</li>
        </ul>
        <h3 style="color: #ffffff;">
          <!--          官网:https://www.yixiang.co-->
        </h3>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" label-position="left"
               label-width="0px">
        <div class="qrHoverBox" />
        <div class="qrBox">
          <VueQr
            :logoScale="0.2"
            :logoMargin="5"
            logoBackgroundColor="white"
            :size="200"
            :margin="0"
            :auto-color="true"
            :dot-scale="1"
            :text="url"
          >
          </VueQr>
          <div  style="font-size: 30px">扫码下载APP</div>
        </div>
        <h3 class="title">
          木瓜商贸管理平台
        </h3>
        <el-form-item prop="companyCode">
          <el-input v-model="loginForm.companyCode" auto-complete="off" placeholder="公司编码" type="text">
            <svg-icon slot="prefix" class="el-input__icon input-icon" icon-class="user"/>
          </el-input>
        </el-form-item>
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" auto-complete="off" placeholder="账号" type="text">
            <svg-icon slot="prefix" class="el-input__icon input-icon" icon-class="user"/>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" auto-complete="off" placeholder="密码" type="password"
                    @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" class="el-input__icon input-icon" icon-class="password"/>
          </el-input>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin:0 0 25px 0;">
          记住我
        </el-checkbox>
        <el-form-item style="width:100%;">
          <el-button :loading="loading" size="medium" style="width:100%;" type="primary"
                     @click.native.prevent="handleLogin">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--  底部  -->
    <div v-if="$store.state.settings.showFooter && !this.isCofco" id="el-login-footer">
      <span v-html="$store.state.settings.footerTxt"/>
      <span> ⋅ </span>
      <a href="http://beian.miit.gov.cn" target="_blank">{{ $store.state.settings.caseNumber }}</a>
    </div>
  </div>
</template>

<script>
import {encrypt} from '@/utils/rsaEncrypt'
import Config from '@/settings'
import Cookies from 'js-cookie'
import VueQr from 'vue-qr'

export default {
  name: 'Login',
  components:{VueQr},
  data() {
    return {
      codeUrl: '',
      url:'',
      cookiePass: '',
      isCofco:false,
      loginForm: {
        companyCode: '',
        username: '',
        password: '',
        rememberMe: false
      },
      loginRules: {
        companyCode: [{required: true, trigger: 'blur', message: '公司编码不能为空'}],
        username: [{required: true, trigger: 'blur', message: '用户名不能为空'}],
        password: [{required: true, trigger: 'blur', message: '密码不能为空'}]
      },
      loading: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCookie()
    this.url = window.location.href.split('/').slice(0,3).join('/')+'/apk/erp.apk'
    //中粮背景
    this.isCofco = window.location.href.includes('bw103-backup')
  },
  methods: {
    getCookie() {
      const companyCode = Cookies.get('companyCode')
      const username = Cookies.get('username')
      let password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      // 保存cookie里面的加密后的密码
      this.cookiePass = password === undefined ? '' : password
      password = password === undefined ? this.loginForm.password : password
      this.loginForm = {
        companyCode: companyCode === undefined ? this.loginForm.companyCode : companyCode,
        username: username === undefined ? this.loginForm.username : username,
        password: password,
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
        // code: ''
      }
    },

    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        const user = {
          companyCode: this.loginForm.companyCode,
          username: this.loginForm.username,
          password: this.loginForm.password,
          rememberMe: this.loginForm.rememberMe
        }
        if (user.password !== this.cookiePass) {
          user.password = encrypt(user.password)
        }
        if (valid) {
          this.loading = true
          if (user.rememberMe) {
            Cookies.set('companyCode', user.companyCode, {expires: Config.passCookieExpires})
            Cookies.set('username', user.username, {expires: Config.passCookieExpires})
            Cookies.set('password', user.password, {expires: Config.passCookieExpires})
            Cookies.set('rememberMe', user.rememberMe, {expires: Config.passCookieExpires})
          } else {
            Cookies.remove('companyCode')
            Cookies.remove('username')
            Cookies.remove('password')
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch('Login', user).then(() => {
            this.loading = false
            //获取通知权限
            //Notification.requestPermission()
            this.$router.push({name:'Notice'})
            // this.$router.push({path: this.redirect || '/'})
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" rel="stylesheet/scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-size: cover;
}
.cofco{
  background-image: url("../assets/images/cofcoBG.png");
}
.other{
  background-image: url("../assets/images/login-background-guoqingjie.jpg");
}

.title {
  margin: 0 auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 385px;
  padding: 25px 25px 5px 25px;
  position: relative;
  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  display: inline-block;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle
  }
}
.qrHoverBox{
  width: 50px;
  height: 50px;
  background: url("");
  position: absolute;
  top: 0;
  right: 0;
  z-index: 11;
}
.qrHoverBox:hover + .qrBox{
  display: block;
}


.qrBox{
  position: absolute;
  z-index: 10;
  width: 90%;
  height: 90%;
  display: none;
  padding:  20px 18%;
  background-color: #ffffff;
}

</style>
